package com_pos.mk;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter.LengthFilter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Pos_mkActivity extends Activity {

	private EditText userIn;
	private EditText passIn;
	private EditText username;
	private EditText email;
	private EditText pass;
	private EditText nick;
	private EditText nick2;
	private EditText big_box;
	private EditText big_box_total;
	private EditText small_box;
	private EditText small_box_total;
	private EditText pcs;
	private EditText total;
	private Button save_button;

	private String user;
	private String pwd;
	private String nickname;
	private String friend;
	private String mail;
	
	private int pcs_total;
	private int small;
	private int small2;
	private int subsmall;
	private int big;
	private int big2;
	private int subbig;
	private int grandtotal;
	
	public static final int MESSAGE_READ = 1;
	public static final int MESSAGE_ONFRIEND = 3;
	public static final int MESSAGE_OFFFRIEND = 4;
	public static final int MESSAGE_SIGNIN_FAIL = 5;
	public static final int MESSAGE_SIGNIN_SUCC = 6;
	public static final int MESSAGE_SIGNOUT = 7;
	public static final int MESSAGE_REGISTER = 8;

	private ArrayAdapter<String> menulist;
	private ArrayAdapter<String> friendAdapter;
	private ArrayAdapter<String> friendOff;

	private pos_module module = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signin);
	}

	@Override
	public void onStart() {
		super.onStart();
		module = new pos_module(this, mHandler);

	}

	// Button handler
	public void clickHandler(View view) {
		switch (view.getId()) {
		case R.id.button_Login:
			userIn = (EditText) findViewById(R.id.userName);
			passIn = (EditText) findViewById(R.id.password);
			user = userIn.getText().toString();
			pwd = passIn.getText().toString();
			module.login(user, pwd);
			break;
		case R.id.calculate_button:
			//Toast.makeText(getApplicationContext(),
			//		"Sorry, your User and Password is not correct!", 15)
			//		.show();
			//break;
			big_box = (EditText)findViewById(R.id.box_besar);
			email = (EditText)findViewById(R.id.email);
			big_box_total=(EditText)findViewById(R.id.box_besar_total);
			big = Integer.parseInt(big_box.getText().toString());
			big2 = Integer.parseInt(email.getText().toString());
			
			/*String ErrBig;
			ErrBig = email.getText().toString();
			//* Kamis , 02/02/2012
			if(ErrBig==""){
				big2=0;
			}*/
			//Toast.makeText(null, big2,30).show();
				
			
			subbig=big*big2;
			big_box_total.setText(Integer.toString(subbig));
			
			small_box=(EditText)findViewById(R.id.box_kecil);
			nick = (EditText)findViewById(R.id.nickName);
			small_box_total=(EditText)findViewById(R.id.box_kecil_total);
			small=Integer.parseInt(small_box.getText().toString());
			small2 = Integer.parseInt(nick.getText().toString());
			
			//* Kamis , 02/02/2012
			if(nick.length()==0){
				small2=0;
			}
			
			subsmall=small*small2;
			small_box_total.setText(Integer.toString(subsmall));
			
			pcs=(EditText)findViewById(R.id.password);
			pcs_total=Integer.parseInt(pcs.getText().toString());
			
			total=(EditText)findViewById(R.id.total);
			
			grandtotal = subbig + subsmall+pcs_total;
			total.setText(Integer.toString(grandtotal));
			save_button=(Button)findViewById(R.id.completeReg_button);
			save_button.setEnabled(true);
			break;
			
		case R.id.completeReg_button:
			username = (EditText) findViewById(R.id.regname);
			email = (EditText) findViewById(R.id.email);
			pass = (EditText) findViewById(R.id.password);
			nick = (EditText) findViewById(R.id.nickName);
			user = username.getText().toString();
			pwd = pass.getText().toString();
			mail = email.getText().toString();
			nickname = nick.getText().toString();
			module.regUser(user, mail, pwd, nickname, "user");
			// setContentView(R.layout.list);
			// onFriendSetup();
			// module.findFriend(user,"on");
			// break;
		}
	}

	// The thread Handler that gets information from the Services and interact
	// w/ UI
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MESSAGE_SIGNIN_SUCC:
				// loginStatus = true;
				setContentView(R.layout.menu);
				setupMenu();
				break;
			case MESSAGE_SIGNIN_FAIL:
				Toast.makeText(getApplicationContext(),
						"Sorry, your User and Password is not correct!", 15)
						.show();
				break;
			case MESSAGE_ONFRIEND:
				String friendlist = new String(msg.obj.toString());
				friendAdapter.add(friendlist);
				break;
			case MESSAGE_OFFFRIEND:
				String offfriendlist = new String(msg.obj.toString());
				friendOff.add(offfriendlist);
				break;
			case MESSAGE_REGISTER:
				// String pesan = new String(msg.obj.toString());
				// Toast.makeText(getApplicationContext(), pesan, 10).show();
				setContentView(R.layout.list);
				onFriendSetup();
				module.findFriend(user, "on");
				break;
			}
		}
	};

	private void setupMenu() {
		// Menyusun menu
		String[] menuList = new String[] { "Stock", "User Active", "Sign Out" };
		// Find and set up the ListView online friends
		menulist = new ArrayAdapter<String>(this, R.layout.main_menu, menuList);
		ListView menu2 = (ListView) findViewById(R.id.menu1);
		menu2.setAdapter(menulist);
		menu2.setOnItemClickListener(menuClickListener);
	}

	private void onFriendSetup() {
		// Find and set up the ListView online friends
		friendAdapter = new ArrayAdapter<String>(this, R.layout.friend_temp);
		ListView online = (ListView) findViewById(R.id.online_list);
		online.setAdapter(friendAdapter);
		online.setOnItemClickListener(friendClickListener);
	}

	private void offFriendSetup() {
		// Find and set up the ListView offline friends
		friendOff = new ArrayAdapter<String>(this, R.layout.friend_off);
		ListView offline = (ListView) findViewById(R.id.offline_list);
		offline.setAdapter(friendOff);
		// offline.setOnItemClickListener(friendClickListener);
	}

	private OnItemClickListener menuClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
			// setContentView(R.layout.message);
			String option = ((TextView) v).getText().toString();
			if (option.equals("Sign Out")) {
				module.setlogout(user);
				setContentView(R.layout.signin);
			} else if (option.equals("Stock")) {
				setContentView(R.layout.list);
				onFriendSetup();
				module.findFriend(user, "on");
			} else if (option.equals("User Active")) {
				setContentView(R.layout.oflist);
				offFriendSetup();
				module.findFriend(user, "off");
			}
		}
	};

	private OnItemClickListener friendClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
			String r, r2;
			setContentView(R.layout.register);
			friend = ((TextView) v).getText().toString();
			r = module.medicinePHP(friend);
			r2 = module.medicinePHP2(friend);
			// Toast.makeText(getApplicationContext(),
			// r,300)
			// .show();
			nick2 = (EditText) findViewById(R.id.regname);
			nick2.setText(friend);
			big_box = (EditText) findViewById(R.id.box_besar);
			big_box.setText(r);
			small_box = (EditText) findViewById(R.id.box_kecil);
			small_box.setText(r2);
			// setupChat();
			// module.getMsg(friend, user,"person","run");
			// roomStatus = false;
		}
	};

}